package org.kabeja.parser.objects;

import org.kabeja.dxf.DXFConstants;
import org.kabeja.dxf.entity.DXFSpatialFilter;
import org.kabeja.dxf.objects.DXFObject;
import org.kabeja.parser.DXFValue;

/**
 * @author shutao
 * @since 2022/4/28 12:28
 */
public class DXFSpatialFilterHandler extends AbstractDXFObjectHandler {
    public static final int POINT_NUMBER = 70;
    public static final int BOUNDARY_POINT_X = 10;
    public static final int BOUNDARY_POINT_Y = 20;
    public static final int NORMAL_STRETCHX = 210;
    public static final int NORMAL_STRETCHY = 220;
    public static final int LOCAL_POINTX = 11;
    public static final int LOCAL_POINTHY = 21;
    public static final int CLIPPING_BOUNDARIES = 71;
    public static final int CLIPPING_FRONT = 72;
    public static final int CLIPPING_FRONT_DISTANCE_OR_MATRIX = 40;
    public static final int CLIPPING_BACK = 73;
    public static final int CLIPPING_BACK_DISTANCE = 41;

    protected DXFSpatialFilter spatialFilter;
    private int transformationMatrixLockCount;

    public DXFSpatialFilterHandler() {
    }

    @Override
    public void endObject() {

    }

    @Override
    public DXFObject getDXFObject() {
        return this.spatialFilter;
    }

    @Override
    public String getObjectType() {
        return DXFConstants.OBJECT_TYPE_SPATIAL_FILTER;
    }

    @Override
    public void parseGroup(int var1, DXFValue var2) {
        switch (var1) {
            case POINT_NUMBER:
                this.spatialFilter.setPointNumber(var2.getIntegerValue());
                break;
            case BOUNDARY_POINT_X:
                this.spatialFilter.setBoundaryPointX(var2.getDoubleValue());
                break;
            case BOUNDARY_POINT_Y:
                this.spatialFilter.setBoundaryPointY(var2.getDoubleValue());
                break;
            case NORMAL_STRETCHX:
                this.spatialFilter.setNormalStretchX(var2.getDoubleValue());
                break;
            case NORMAL_STRETCHY:
                this.spatialFilter.setNormalStretchY(var2.getDoubleValue());
                break;
            case LOCAL_POINTX:
                this.spatialFilter.setLocalPointX(var2.getDoubleValue());
                break;
            case LOCAL_POINTHY:
                this.spatialFilter.setLocalPointY(var2.getDoubleValue());
                break;
            case CLIPPING_BOUNDARIES:
                this.spatialFilter.setClippingBoundaries(var2.getIntegerValue());
                break;
            case CLIPPING_FRONT:
                this.spatialFilter.setClippingFront(var2.getIntegerValue());
                this.transformationMatrixLockCount++;
                break;
            case CLIPPING_FRONT_DISTANCE_OR_MATRIX:
                if (this.transformationMatrixLockCount == 2) {
                    // 添加矩阵
                        this.spatialFilter.setMatrix(var2.getDoubleValue());
                } else {
                    if (this.spatialFilter.isClippingFront()) {
                        this.spatialFilter.setClippingFrontDistance(var2.getDoubleValue());
                    }
                    this.transformationMatrixLockCount++;
                }
                break;
            case CLIPPING_BACK:
                this.spatialFilter.setClippingBack(var2.getIntegerValue());
                break;
            case CLIPPING_BACK_DISTANCE:
                if (this.spatialFilter.isClippingBack()) {
                    this.spatialFilter.setClippingBackDistance(var2.getDoubleValue());
                }
                break;
            default:
                super.parseCommonGroupCode(var1, var2, this.spatialFilter);
        }
    }

    @Override
    public void startObject() {
        this.spatialFilter = new DXFSpatialFilter();
        this.transformationMatrixLockCount = 0;
    }
}
